﻿using System.Data;
using ExampleApplication.Models;
using SQLPXBase;
using SQLPXBase.Factory.Keys;

namespace ExampleApplication.SQLProxy
{
    internal class sql : SQLPXBase.SQLProxyBase
    {
        public sql() { }

        internal class cCar:SQLPXBase.SqlProxySubclassBase
        {
            public cCar(SQLProxyBase sqlproxy) : base(sqlproxy)
            {
            }

            public DataRow Load(LongPK id)
            {
                ProxyCommand cmd = new ProxyCommand("dbo.CAR_Load");
                cmd.AddParameters(id);
                return sqlProxy.ExecuteSingleResult(cmd);
            }

            public int Save(Person p)
            {
                ProxyCommand cmd = new ProxyCommand("dbo.PERSON_Save");
                cmd.AddParameters(p.Id, p.Name, p.Surname);
                sqlProxy.ExecuteNoQuery(cmd);

                return (int)cmd.OutputParameters.GetByIndex(0);
            }

            public DataTable ListByOwner(LongPK ownerId)
            {
                ProxyCommand cmd = new ProxyCommand("dbo.CAR_ListByOwner");
                cmd.AddParameters(ownerId);
                return sqlProxy.ExecuteDataTable(cmd);
            }
        }

        internal class cPerson : SQLPXBase.SqlProxySubclassBase
        {
            public cPerson(SQLProxyBase sqlproxy)
                : base(sqlproxy)
            {
            }

            public DataRow Load(LongPK id)
            {
                ProxyCommand cmd = new ProxyCommand("dbo.PERSON_Load");
                cmd.AddParameters(id);
                return sqlProxy.ExecuteSingleResult(cmd);
            }

            public DataTable List()
            {
                ProxyCommand cmd = new ProxyCommand("dbo.PERSON_List");
                return sqlProxy.ExecuteDataTable(cmd);

            }
        }

        internal cCar Car;
        internal cPerson Person;

        public sql(SQLConnectionString connStr) : base(connStr)
        {
            this.Car = new cCar(this);
            this.Person = new cPerson(this);

        }
    }
}
